package com.htkj.demo;

import android.os.Handler;
import android.os.Looper;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtlis {
    private static ExecutorService mParallelExecutor = new ThreadPoolExecutor(10, 10,
            60L, TimeUnit.SECONDS,
            new SynchronousQueue<Runnable>());//最多为5个线程  可以并发执行(也就是说最多只有5个线程同时运行，超过5个的就要等待)  异步线程池
    // private static Executor mSerialExecutor = AsyncTask.SERIAL_EXECUTOR;//按照顺序执行  同步线程池(系统默认使用的)
    private static Handler sHandler = new Handler(Looper.getMainLooper());

    private ThreadPoolUtlis() {
        //  mParallelExecutor = THREAD_POOL_EXECUTOR;
        //    mSerialExecutor = AsyncTask.SERIAL_EXECUTOR;
    }

    public static void ui(Runnable runnable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            runnable.run();
        }else {
            sHandler.post(runnable);
        }
    }

    public static Handler getsHandler() {
        return sHandler;
    }

    public static void asyn(Runnable task) {
        mParallelExecutor.execute(task);
    }


    public static void shutdown(){
        mParallelExecutor.shutdown();
    }

    public static Future<?> submit(Runnable runnable){
        return mParallelExecutor.submit(runnable);
    }


}
